package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.WebViewActivity;
import com.sunday.android.fangdaifu.bukong.entity.NewsImage;
import com.sunday.android.fangdaifu.bukong.entity.NewsItem;
import com.sunday.android.fangdaifu.bukong.utils.Constants;
import com.sunday.android.fangdaifu.bukong.utils.ViewPagerUtils;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/7/21.
 */

public class NewsListAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<NewsItem> newsItemList,imgItemList;
    private int w;

    public NewsListAdapter(Context context,List<NewsItem> newsItemList,List<NewsItem> imgItemList) {
        this.context = context;
        this.newsItemList = newsItemList;
        this.imgItemList = imgItemList;
        w = DeviceUtils.getDisplay(context).widthPixels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.item_banner, parent, false);
            return new TopHolder(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.item_date_child, parent, false);
            return new ItemHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position==0){
            TopHolder topHolder = (TopHolder) holder;
            if (!imgItemList.isEmpty()){
                new ViewPagerUtils(context,topHolder.banner,topHolder.tvInfo,imgItemList,w);
            }
        }else {
            ItemHolder holder1 = (ItemHolder) holder;
            final NewsItem newsItem = newsItemList.get(position-1);
            holder1.title.setText(newsItem.getSummary());
            holder1.time.setText(newsItem.getCreateTime());
            if(!TextUtils.isEmpty(newsItem.getImage())){
                Picasso.with(context).
                        load(newsItem.getImage()).
                        resize(240,  120).
                        placeholder(R.drawable.default_error).into(holder1.img);
            }
            holder1.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra(Constants.TITLE,Constants.NEW_TITLE);
                    intent.putExtra(Constants.URL,newsItem.getContext());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return newsItemList.size()+1;
    }

    class TopHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.banner)
        ConvenientBanner banner;
        @Bind(R.id.tvInfo)
        TextView tvInfo;
        public TopHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.item)
        RelativeLayout item;
        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
